//
//  CLrightImageTextField.m
//  greatControl
//
//  Created by Phyllis Hu on 15/10/21.
//  Copyright (c) 2015年 dingyi. All rights reserved.
//

#import "CLRightImageTextField.h"
#import "UIView+Extension.h"
#import "CLUIbutton.h"
#import "NSString+composed.h"
#import "UIImage+Fit.h"
#define btnTitleFont  16

@interface CLRightImageTextField()

@property (nonatomic,assign) CGFloat btnTitleW;
@property (nonatomic,assign) CGFloat btnImageW;
@end

@implementation CLRightImageTextField

- (id)initWithFrame:(CGRect)frame rightNorImage:(NSString *) rightNorImage rightHighlightImage:(NSString *)rightHighlightImage rightTitle:(NSString*) rightTitle
{
    self = [super initWithFrame:frame];
    if (self) {
        // 设置背景
        self.background = [UIImage resizeImage:@"garyBox"];
        
        _btnTitleW = [self getBtnTitleW:rightTitle];
        // 设置左边显示的图标
        UIButton * btn = [self greatRightBtnNorImage:rightNorImage HighlightImage:rightHighlightImage title:rightTitle];
        _rightBtn = (CLUIbutton*)btn;
    
        // 设置leftView的内容居中
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 35)];
        UILabel *leftLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 200, 35)];
        [leftLabel setBackgroundColor:[UIColor clearColor]];
        leftLabel.textAlignment = NSTextAlignmentLeft;
        [leftLabel setTextColor:[UIColor grayColor]];
         _leftLabel = leftLabel;
        [view addSubview:leftLabel];
        self.leftView = view;
        
        self.rightView = btn;
        [self.rightView setBackgroundColor:[UIColor clearColor]];
        
        // 设置右边的view永远显示
        self.rightViewMode = UITextFieldViewModeAlways;
    }
    return self;
}

/**
 *  计算右边按钮文字的宽度
 */
- (CGFloat) getBtnTitleW:(NSString*) rightTitle{
    
    CGFloat btnTitleW = 0;
    if (_btnFont) {
        btnTitleW = [rightTitle sizeWithMyFont:[UIFont systemFontOfSize:_btnFont]].width;
    }else{
        btnTitleW = [rightTitle sizeWithMyFont:[UIFont systemFontOfSize:btnTitleFont]].width;
    }
    return btnTitleW;
}

/**
 *  创建右边的UIButton
 */
- (UIButton*)greatRightBtnNorImage:(NSString *) norImage HighlightImage:(NSString *)HighlightImage title:(NSString*) title{

    
    UIImage *btnImage = nil;
    _btnImageW = 0;
    if (norImage) {
        btnImage = [UIImage imageNamed:norImage];
        _btnImageW = 18;
    }
    
//    if (self.allClick) {
        _btnTitleW = self.frame.size.width - _btnImageW ;
//    }
    CLUIbutton *btn = [[CLUIbutton alloc]initImageW:_btnImageW titleW:_btnTitleW hight:self.height];
    [btn setBackgroundColor:[UIColor clearColor]];
    [btn setImage: btnImage forState:UIControlStateNormal];
    [btn setTitle:title forState:UIControlStateNormal];
//    [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    if (_btnFont) {
        btn.titleLabel.font = [UIFont systemFontOfSize:_btnFont];
    }else{
        btn.titleLabel.font = [UIFont systemFontOfSize:btnTitleFont];
    }
    
    [btn addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
    return btn;
}

/**
 *  设置右边按钮的文字
 */
- (void) setRightBtnTitle:(NSString*) title{
    [self.rightBtn setTitle:title forState:UIControlStateNormal];
    _btnTitleW = [self getBtnTitleW:title];
    self.rightBtn.width = _btnTitleW+_btnImageW;
    [self.rightBtn againLayoutBtnImageW:_btnImageW BtnTitleW:_btnTitleW];
}

/**
 *  按钮点击事件
 */
- (void) onClick:(UIButton*)sender{
    if ([self.btnDelegate respondsToSelector:@selector(RightImageTextFieldBtnOnClick:)]) {
        [self.btnDelegate RightImageTextFieldBtnOnClick:self];
    }
}


@end
